package animationGame;

import java.awt.Graphics;

public class PaintableStar extends GeometricObject implements Paintable {
	public int numSpikes;
	public double outerRadius;
	public double innerRadius;

	public PaintableStar(int numSpikes, double outerRadius, double innerRadius,
			double x, double y) {
		super(outerRadius * 2, outerRadius * 2, new Vertex(x, y));
		this.numSpikes = numSpikes;
		this.outerRadius = outerRadius;
		this.innerRadius = innerRadius;
	}

	public void paintTo(java.awt.Graphics g) {
		int[] x = new int[numSpikes * 2];
		int[] y = new int[numSpikes * 2];
		double minAng = 360.0 / (numSpikes * 2.0);
		double[] temp = { 0.0, 0.0 };
		for (int i = 0; i < x.length; i++) {
			//if (i % 2 == 0) {
				temp = circleCoordinates(pos.x, pos.y, outerRadius, i * minAng);
			/*} else {
				System.out.println("test");
				temp = circleCoordinates(pos.x + outerRadius - innerRadius,
						pos.y + outerRadius - innerRadius, innerRadius, i
								* minAng);
			}*/
			x[i] = (int) temp[0];
			y[i] = (int) temp[1];
		}
		System.out.println("Schleifenende");

		g.fillPolygon(x, y, numSpikes * 2);
		//g.fillRect(55, 55, 55, 55);
	}

	// returns a position on the circle with a specific angle
	private double[] circleCoordinates(double x1, double y1, double r,
			double ang) {
		
		double x = 0, y = 0;
		if (ang < 0 || ang > 360) {
			System.out.println("Falscher Winkel!");
			return null;
		}
		if(ang == 0.0){
			y = r;
			x = 0.0;
		}else if(ang == 90.0){
			y = 0.0;
			x = r;
		}else if(ang == 180.0){
			y = -r;
			x = 0.0;
		}else if(ang == 270.0){
			y = 0.0;
			x = -r;
		}else if (ang < 90) {
			ang = 90.0 - ang;
			y = r * Math.sin(ang);
			x = Math.sqrt(r * r - y * y);
		} else if (ang < 180) {
			ang = ang - 90.0;
			y = r * Math.sin(ang);
			x = Math.sqrt(r * r - y * y);
			y = -y;
		} else if (ang < 270) {
			ang = 270.0 - ang;
			y = r * Math.sin(ang);
			x = -Math.sqrt(r * r - y * y);
			y = -y;
		} else if (ang <= 360) {
			ang = ang - 270.0;
			y = r * Math.sin(ang);
			x = -Math.sqrt(r * r - y * y);
		}
		System.out.println("Winkel: " + ang);
		System.out.println("Radius: " + r);
		System.out.println(x + " : " + y);
		double[] res = { x1 + r + x, y1 + r - y };
		return res;
	}

	public static void main(String[] args) {
		ShowInFrame
				.show(new PaintablePanel(new PaintableStar(8, 100, 50, 100, 100)));
	}

}
